/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.adgroup_ad_service;

import java.util.ArrayList;

import com.hiddenequity.acts.Act_Super;
import com.hiddenequity.utils.Converter;
import com.hiddenequity.utils.SOAPUtils;
import com.hiddenequity.utils.UrlBuncher;

public class AdGroupAdService {

	public static final String URL_ADGROUP_AD_SERVICE = UrlBuncher.URL_BASE + "AdGroupAdService";
	
	/*
	 * Add text ad
	 */
	public static String textAdAddOperation(String adGroupId, String headline, 
			String description1, String description2, String displayUrl, String url){
		
		String SOAPBody = 
				"<soap:Body>" +
					"<mutate xmlns=" + SOAPUtils.XMLNS +
					"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
					"<operations>" +
						"<operator>ADD</operator>" +
						"<operand>" +
						 	"<adGroupId>" + adGroupId + "</adGroupId>" +
							"<ad xsi:type='TextAd'>" +
								"<url>" + Converter.escapeXML(url) + "</url>" +
								"<displayUrl>" + Converter.escapeXML(displayUrl) + "</displayUrl>" +
								"<headline>" + Converter.escapeXML(headline) + "</headline>" +
								"<description1>" + Converter.escapeXML(description1) +"</description1>" +
								"<description2>" + Converter.escapeXML(description2) +"</description2>" +	
							"</ad>" +
						"</operand>" +
					"</operations>" +
				"</mutate>" +
			"</soap:Body>";
		
			String envelope = SOAPUtils.buildEnvelope( SOAPBody);
			return envelope;
	}//addTextAd
	
	
	
	/*
	 * Get ad by id
	 */
	public static String getTextAdById(String adId){
		String SOAPBody = 
			"<soap:Body>" +
				"<get xmlns=" + SOAPUtils.XMLNS +
					"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
					"<serviceSelector>" +
						"<fields>Status</fields>" +	
						"<predicates>" +
							"<field>Id</field>" +
							"<operator>EQUALS</operator>" +
							"<values>" + adId + "</values>" +
						"</predicates>" +
					"</serviceSelector>" +
				"</get>" +
			"</soap:Body>";
			String envelope = SOAPUtils.buildEnvelope( SOAPBody);
			return envelope;
	}
	
	
	
	
	
	/*
	 * Get ad list
	 */
	public static String getAdList(String adGroupId, ArrayList<String> dateRange, int status){
		String SOAPBody = 
				"<soap:Body>" +
					"<get xmlns=" + SOAPUtils.XMLNS +
					"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
					"<serviceSelector>" +
						"<fields>Status</fields>" +
						"<fields>Clicks</fields>" +
						"<fields>Cost</fields>" +
						"<fields>Ctr</fields>" +
						"<fields>Impressions</fields>" +
						"<fields>AverageCpc</fields>";		
						
		if(status != 0){
			String statusName = "";
			switch (status) {
				case Act_Super.STATUS_ACTIVE:
					statusName = "ENABLED";
					break;
				case Act_Super.STATUS_PAUSED:
					statusName = "PAUSED";
					break;
				case Act_Super.STATUS_DELETED:
					statusName = "DISABLED";
					break;
			}
			
			SOAPBody = SOAPBody + 
				"<predicates>" +
					"<field>Status</field>" +
					"<operator>EQUALS</operator>" +
					"<values>" + statusName + "</values>" +
				"</predicates>";
		}	
						
		SOAPBody = SOAPBody + "<predicates>" +
							"<field>AdGroupId</field>" +
							"<operator>EQUALS</operator>" +
							"<values>" + adGroupId + "</values>" +
						"</predicates>";
		
		if(!dateRange.get(0).equals("")){
			SOAPBody = SOAPBody +	"<dateRange>" +
					"<min>" + dateRange.get(0) +"</min>" +
					"<max>" + dateRange.get(1) +"</max>" +
					"</dateRange>";
		}
		
		SOAPBody = SOAPBody + 
						"<ordering>" +
							"<field>Clicks</field>" +
							"<sortOrder>DESCENDING</sortOrder>" +
						"</ordering>" +
					"</serviceSelector>" +
				"</get>" +
			"</soap:Body>";
		
		String envelope = SOAPUtils.buildEnvelope( SOAPBody);
		return envelope;
		
	}

	

	/*
	 * Remove Ad operation
	 */
	public static String textAdRemoveOperation(String adGroupId, String adId) {
		
		String SOAPBody = 
				"<soap:Body>" +
					"<mutate xmlns=" + SOAPUtils.XMLNS +
						"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
						"<operations>" +
							"<operator>REMOVE</operator>" +
							"<operand>" +
								"<adGroupId>" + adGroupId + "</adGroupId>" +
									"<ad xsi:type='TextAd'>" +
										"<id>" + adId + "</id>" +
									"</ad>" +
							"</operand>" +
						"</operations>" +
					"</mutate>" +
				"</soap:Body>";		
		
		String envelope = SOAPUtils.buildEnvelope(SOAPBody);
		return envelope;		
	}//textAdRemoveOperation
	
	
	
	/*
	 * Set ad status
	 */
	static public String setAdStatus(String adGroupId, String adId, int status){
		String statusName = "";
		
		if(status != 0){
			switch (status) {
				case Act_Super.STATUS_ACTIVE:
					statusName = "ENABLED";
					break;
				case Act_Super.STATUS_PAUSED:
					statusName = "PAUSED";
					break;
				case Act_Super.STATUS_DELETED:
					statusName = "DISABLED";
					return textAdRemoveOperation(adGroupId, adId);
			}
		}
		
		String SOAPBody = 
			"<soap:Body>" +
				"<mutate xmlns=" + SOAPUtils.XMLNS +
					"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
					"<operations>" +
						"<operator>SET</operator>" +
						"<operand>" +
							"<adGroupId>" + adGroupId + "</adGroupId>" +
								"<ad xsi:type='TextAd'>" +
									"<id>" + adId + "</id>" +
								"</ad>" +
							"<status>"+ statusName + "</status>" +
						"</operand>" +
					"</operations>" +
				"</mutate>" +
			"</soap:Body>";		
		
		String envelope = SOAPUtils.buildEnvelope(SOAPBody);
		return envelope;
	}//setAdGroupStatus




}
